package com.zuimei.zuimei.widgets;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.zuimei.zuimei.utils.ScreenUtil;

/**
 * Created by Jogger on 2017/1/25.
 * 自定义详情页scrollview
 */

public class MyScrollView extends ScrollView {
    boolean isShow = true;//判断圆按钮是否显示
    boolean isOver = false;//判断按钮是否已经和回退按钮同级（BCD处于显示状态）
    private int mStartY;
    private int mFirstPosition;
    private OnScrollListener mListener;
    private int mUpShowY;//上滑到此处显示控件A，隐藏B,C,D
    private int mDownShowY;//下滑到此处显示BCD,隐藏A
    private boolean mIsEnlarge = false;//是否放大图片
    private ImageView mImageView;//要放大操作的图片
    private boolean mIsLarging;//是否正在放大状态
    private int mImgWidth;
    private int mImgHeight;
    private int mMY = -1;


    public interface OnScrollListener {
        void showAppBtn();//移入按钮

        void hideAppBtn();//移出按钮

        void upShow();

        void downShow();

    }

    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        ScreenUtil.getSrceenHeight(context);
    }

    public void setOnScrollListener(int upShowY, int downShowY, OnScrollListener listener) {
        mListener = listener;
        mUpShowY = upShowY;
        mDownShowY = downShowY;
        Log.d("TAG","---UP"+upShowY+"down"+downShowY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mStartY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (mMY == -1) {
                    mMY = (int) ev.getY();
                }
                int moveY = (int) ev.getY();
                int scrollY = getScrollY();
                if (mStartY - moveY > 50) {
                    //向上移动
                    if (isShow) {
                        if (mListener != null) {
                            mListener.showAppBtn();
                        }
                        isShow = false;
                    }
                    mStartY = moveY + 50;
                    if (!isOver && scrollY <= mUpShowY) {
                        if (scrollY >= mDownShowY) {
                            if (mListener != null) {
                                mListener.upShow();
                            }
                            isOver = true;
                        }
                    }
                } else {
                    //下滑
                    if (!isShow) {
                        mListener.hideAppBtn();
                        isShow = true;
                    }
                    mStartY = moveY + 50;
                    if (isOver) {
                        if (scrollY <= mDownShowY) {
                            //滑动到与回退按钮底部
                            if (mListener != null) {
                                mListener.downShow();
                            }
                            isOver = false;
                        }
                    }
                }

                //处理放大
                if (mIsEnlarge) {
                    if (moveY - mMY > 5) {
                        //下滑
                        if (!mIsLarging) {
                            if (getScrollY() == 0) {
                                mFirstPosition = (int) ev.getY();
                            } else {
                                break;
                            }
                        }
                        //放大
                        mIsLarging = true;
                        largeAnim(moveY - mFirstPosition);
                        return true;
                    } else {
                        mIsLarging = false;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                if (mIsEnlarge) {
                    reStore();
                    mMY = -1;
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    private void largeAnim(int moveY) {
        ViewGroup.LayoutParams layoutParams = mImageView.getLayoutParams();
        layoutParams.height = mImgHeight + moveY / 3;//设置高度
        if (layoutParams.height > mImgHeight * 2) {//设置界限
            layoutParams.height = mImgHeight * 2;
        }
        mImageView.setLayoutParams(layoutParams);
        mImageView.setScaleX(layoutParams.height / (float) mImgHeight);//等比设置宽度
    }

    /**
     * 重置控件
     */
    private void reStore() {
        final ViewGroup.LayoutParams layoutParams = mImageView.getLayoutParams();
        ValueAnimator valueAnimator = ValueAnimator.ofInt(layoutParams.height, mImgHeight);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int h = (int) animation.getAnimatedValue();
                layoutParams.height = h;
                mImageView.setLayoutParams(layoutParams);

                mImageView.setScaleX((float) h / mImgHeight);
            }
        });
        valueAnimator.start();
        mIsLarging = false;
    }

    public void setEnlarge(ImageView imageView) {
        mIsEnlarge = true;
        mImageView = imageView;
        mImageView.measure(0, 0);
        mImgWidth = mImageView.getMeasuredWidth();
        mImgHeight = mImageView.getMeasuredHeight();
    }
}
